package com.ax.base.common.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ax.base.common.domain.BaseSubject;
import com.ax.base.common.domain.Tree;
import com.ax.base.system.domain.MenuDO;


@Mapper
public interface BaseSubjectMapper {
    int deleteByPrimaryKey(Long subjectId);

    int insert(BaseSubject record);

    /**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param BaseSubject baseSubject
	 *           
	 * @return SUCC
	 */
    @Insert("<script>"+
		"insert into base_subject (Subject_ID, Subject_Code, Subject_Name,"+ 
        "First_Letter, Parent_Subject_ID, Remarks,"+ 
        "Create_date, Update_date, Flag"+
        ")"+
        "values (#{subjectId}, #{subjectCode}, #{subjectName},"+ 
        "#{firstLetter}, #{parentSubjectId}, #{remarks},"+
        "NOW(), #{updateDate}, #{flag}"+
        ")"+
        "</script>")
    int insertSelective(BaseSubject record);

    /**
	 * 医学科目(模块:医学科目)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseSubject baseSubject
	 */
    @Select("<script>"+
    		"select "+ 
			"Subject_ID menuId, Subject_Code, Subject_Name name, First_Letter, Parent_Subject_ID parentId, Remarks,"+ 
		    "Create_date, Update_date, Flag "+
		    "from base_subject"+
		    "<where>" + 
		  	"<if test=\"subjectId != null\">"+ "and Subject_ID = #{subjectId} " + "</if>" + 
		  	"</where>"+
		  	"</script>")
    BaseSubject selectByPrimaryKey(@Param("subjectId") Long subjectId);
    
    /**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param BaseSubject baseSubject
	 *           
	 * @return SUCC
	 */
    @Update("<script>"+
    		"update base_subject"+
    	    "<set>"+
    	    "<if test=\"subjectCode != null and subjectCode != ''\">"+ "Subject_Code = #{subjectCode}, " + "</if>" +
    	    "<if test=\"subjectName != null and subjectName != ''\">"+ "Subject_Name = #{subjectName}, " + "</if>" +
    	    "<if test=\"firstLetter != null and firstLetter != ''\">"+ "First_Letter = #{firstLetter}, " + "</if>" +
    	    "<if test=\"parentSubjectId != null\">"+ "Parent_Subject_ID = #{parentSubjectId}, " + "</if>" +
    	    "<if test=\"remarks != null and remarks != ''\">"+ "Remarks = #{remarks}, " + "</if>" +
    	    "<if test=\"createDate != null\">"+ "Create_date = #{createDate}, " + "</if>" +
    	    "Update_date = NOW()," +
    	    "<if test=\"flag != null\">"+ "Flag = #{flag}, " + "</if>" +
    	    "</set>"+
    	    "where Subject_ID = #{subjectId,jdbcType=BIGINT}"+
    	    "</script>")
    int updateByPrimaryKeySelective(BaseSubject record);

    int updateByPrimaryKey(BaseSubject record);
    
    /**
	 * 医学科目(模块:医学科目)
	 * 
	 * @param Map<String,Object> map
	 *           
	 * @return List<BaseSubject>
	 */
    @Select("<script>"+
    		"select "+ 
			"Subject_ID menuId, Subject_Code, Subject_Name name, First_Letter, Parent_Subject_ID parentId, Remarks,"+ 
		    "Create_date, Update_date, Flag "+
		    "from base_subject"+
		    "<where>" + 
		  	"<if test=\"subjectId != null\">"+ "and Subject_ID = #{subjectId} " + "</if>" + 
		  	"</where>"+ 
		  	"</script>"
	         )
    List<BaseSubject> queryBaseSubjectMapper(Map<String,Object> map);
    
    @Select("SELECT Subject_ID id,Subject_Name text, Parent_Subject_ID parentId FROM base_subject")
    List<Tree<MenuDO>> listSubject();
    
    @Select("SELECT Subject_ID id,concat_ws('-',Subject_Name,Subject_Code) text, Parent_Subject_ID parentId FROM base_subject")
    List<Tree<MenuDO>> listTree();
    
    /**
	 * 名字是否重复(新增)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
    @Select("<script>"+
    		"select "+ 
			"Subject_ID menuId, Subject_Code, Subject_Name name, First_Letter, Parent_Subject_ID parentId, Remarks,"+ 
		    "Create_date, Update_date, Flag  "+
		    "from base_subject where Subject_Name = #{subjectName} and Parent_Subject_ID = #{parentSubjectId}"+
		  	"</script>"
	         )
    List<BaseSubject> validateMapper(Map<String,Object> map);
    
    /**
	 * 名字是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
    @Select("<script>"+
    		"select "+ 
			"Subject_ID menuId, Subject_Code, Subject_Name name, First_Letter, Parent_Subject_ID parentId, Remarks,"+ 
		    "Create_date, Update_date, Flag  "+
		    "from base_subject where Subject_Name = #{subjectName} and Parent_Subject_ID = #{parentSubjectId} and Subject_ID != #{subjectId}"+
		  	"</script>"
	         )
    List<BaseSubject> validateUpdateMapper(Map<String,Object> map);
}